package com.xingfly.security.jwt.service;

import com.xingfly.model.User;
import com.xingfly.security.jwt.factory.JwtUserFactory;
import com.xingfly.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Created by SuperS on 2016/12/21.
 */
@Service
public class JwtUserDetailsService implements UserDetailsService {
    @Autowired
    private AccountService accountService;

    @Override
    public UserDetails loadUserByUsername(String usernmae) throws UsernameNotFoundException {
        User user = accountService.find(usernmae);
        if (user == null) {
            throw new UsernameNotFoundException("找不到该用户:" + usernmae);
        }
        return JwtUserFactory.create(user);
    }
}
